//
//  WH_AddViewController.m
//  App_6.23
//
//  Created by wanghao on 16/7/20.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_AddViewController.h"
#import "WH_Address.h"

@interface WH_AddViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *provinceTF;
@property (nonatomic, strong) UITextField *cityTF;
@property (nonatomic, strong) UITextField *postcodeTF;
@property (nonatomic, strong) UITextField *addressTF;
@property (nonatomic, strong) UITextField * countryLabel;
@property (nonatomic, copy) NSString *body;
@property (nonatomic, copy) NSString *btnTitle;
@property (nonatomic, copy) NSString *addressD;


@end

@implementation WH_AddViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [self.tabBarController.tabBar setHidden:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    _addressD = @"0";
    [self createTF:_editAddress];
    [self createDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonActionA)];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
    if ([self.UrlStr isEqualToString:@"address_edit"]) {
        [self createbtn];
        
    }else{
        UIButton *btn = (UIButton *)[self.view viewWithTag:5000];
        [btn removeFromSuperview];
    }

}

-(void)createTF:(WH_Address *)model{
    
    NSMutableArray *placeArr = [NSMutableArray arrayWithObjects:@"name", @"Telephone", @"province", @"city", @"address", @"postcode", @"countryLabel", nil];
    NSMutableArray *nameLabelArr = [NSMutableArray arrayWithObjects:@"name", @"Telephone", @"province", @"city", @"address", @"postcode", @"country", nil];

    for (int i = 0; i < placeArr.count; i++) {
        
        UITextField *textFT = [[UITextField alloc] initWithFrame:CGRectMake(90, 15 + (45*i), SCREEN_WIDTH-90-10, 40)];
        textFT.tag = 4000 + i;
        textFT.borderStyle = UITextBorderStyleRoundedRect;
        textFT.backgroundColor = [UIColor whiteColor];
        textFT.clearButtonMode = UITextFieldViewModeWhileEditing;
        textFT.delegate = self;
        [textFT setPlaceholder:placeArr[i]];
        [self.view addSubview:textFT];
    }
    for (int i = 0; i < nameLabelArr.count; i++) {
        
        UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(10, 15 + (45*i), 75, 40)];
        nameL.text = nameLabelArr[i];
        nameL.font = [UIFont systemFontOfSize:15.f];
        [self.view addSubview:nameL];
    }
    
    self.nameTF = (UITextField *)[self.view viewWithTag:4000];
    self.phoneTF = (UITextField *)[self.view viewWithTag:4001];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;

    self.provinceTF = (UITextField *)[self.view viewWithTag:4002];
    self.cityTF = (UITextField *)[self.view viewWithTag:4003];
    self.addressTF = (UITextField *)[self.view viewWithTag:4004];
    self.postcodeTF = (UITextField *)[self.view viewWithTag:4005];
    _postcodeTF.keyboardType = UIKeyboardTypePhonePad;

    self.countryLabel = (UITextField *)[self.view viewWithTag:4006];

    self.nameTF.text = model.address_name;
    self.phoneTF.text = model.address_tel;
    self.provinceTF.text = model.address_province;
    self.cityTF.text = model.address_city;
    self.postcodeTF.text = model.address_postcode;
    self.countryLabel.text = model.address_country;
    self.addressTF.text = model.address_detail;
}

- (void)createDefault{
    
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(10, 15 + (45*7), 170, 40)];
    nameL.text = @"Default address";
    nameL.font = [UIFont systemFontOfSize:15.f];
    [self.view addSubview:nameL];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH - 30, 15 + (45*7) + 10, 20, 20);
    
    if ([_editAddress.address_default isEqualToString:@"1"]) {
        
        button.selected = YES;
    }
    
    [button setBackgroundImage:[UIImage imageNamed:@"圆形未选中"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"圆形选中"] forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(adressAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
- (void)adressAction:(UIButton *)btn{
    
    btn.selected = !btn.selected;

    if (btn.selected == YES) {
        
        _addressD = @"1";

    }else{
        
        _addressD = @"0";

    }
    
}
//当点击键盘上的return键时触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)createbtn{
    
    UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    delBtn.frame =  CGRectMake(0, 15 + (45*8), SCREEN_WIDTH, 40);
    delBtn.tag = 5000;
    delBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
    [delBtn setTitle:@"删除地址" forState:UIControlStateNormal];
    delBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:delBtn];
    [delBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(delAddress) forControlEvents:UIControlEventTouchUpInside];
    

}



-(void)delAddress{
    
    
    NSString *body = [NSString stringWithFormat:@"action=address_del&user_id=%@&address_id=%@", NSUSERID, _editAddress.address_id];

    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSString *status = [dic objectForKey:@"status"];
        NSString *message = [dic objectForKey:@"message"];
        if ([status isEqualToString:@"success"]) {
            
            [WSProgressHUD showImage:nil status:status];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"打水吧");
                [self.navigationController popViewControllerAnimated:YES];

            });
        }else{
            
            [WSProgressHUD showImage:nil status:message];
        }
        
    } failure:^(NSError *error) {
        
        
    }];

    
}
-(void)rightButtonActionA{
    
    NSLog(@"address default %@",_addressD);
    [self postData:_UrlStr];//address_id

}
-(void)postData:(NSString *)str{
    
    if ([str isEqualToString:@"address_edit"]) {
        
        self.body = [NSString stringWithFormat:@"action=%@&user_id=%@&address_name=%@&address_tel=%@&address_detail=%@&address_city=%@&address_province=%@&address_country=%@&address_postcode=%@&address_default=%@&address_id=%@",str,NSUSERID,_nameTF.text,_phoneTF.text,_addressTF.text,_cityTF.text,_provinceTF.text,_countryLabel.text,_postcodeTF.text,_addressD,_editAddress.address_id];
 
    }else{
        
        self.body = [NSString stringWithFormat:@"action=%@&user_id=%@&address_name=%@&address_tel=%@&address_detail=%@&address_city=%@&address_province=%@&address_country=%@&address_postcode=%@&address_default=%@",str,NSUSERID,_nameTF.text,_phoneTF.text,_addressTF.text,_cityTF.text,_provinceTF.text,_countryLabel.text,_postcodeTF.text,_addressD];
    }
    
    [WH_AppTools postUrl:URLstr body:_body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSString *status = [dic objectForKey:@"status"];
        NSString *message = [dic objectForKey:@"message"];
        if ([status isEqualToString:@"success"]) {
            
            [WSProgressHUD showImage:nil status:@"成功"];
            [self.navigationController popViewControllerAnimated:YES];

        }else{
            
            [WSProgressHUD showImage:nil status:message];
        }
        
    } failure:^(NSError *error) {
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
